package com.zlx.sharelive.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.NearBean;
import com.zlx.module_base.base_api.res_data.NearBody;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.Constants;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 附近的人
 */
public class NearbyAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private int page = 1;
    private List<NearBean.InfoDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<NearBean.InfoDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_nearby;
    }

    @Override
    public void initViews() {
        super.initViews();

        initRecyclerView();
        getNearData();
    }

    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CommonRecyclerViewAdapter<NearBean.InfoDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, NearBean.InfoDTO entity, int position) {
                GlideImgManager.glideLoader(NearbyAc.this, entity.getAvatar(), holder.getView(R.id.iv_head), 0);
                holder.setText(R.id.tv_nickname, entity.getNickname());
                if (!StringUt.isEmpty(entity.getSex())) {
                    if (entity.getSex().equals("1")) {
                        //男
                        holder.setText(R.id.tv_sex, "♂");
                        ((TextView) holder.getView(R.id.tv_sex)).setTextColor(getResources().getColor(R.color.blue));
                    } else {
                        holder.setText(R.id.tv_sex, "♀");
                        ((TextView) holder.getView(R.id.tv_sex)).setTextColor(getResources().getColor(R.color.hot_pink));
                    }
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_nearby2;
            }
        };
        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                String id = (String) SPUtil.get(NearbyAc.this, Constants.SP_ID, "");
                if (id.equals(mList.get(position).getId())) {
                    //如果是自己
                    startActivity(new Intent(NearbyAc.this, HomepageAc.class));
                } else {
                    startActivity(new Intent(NearbyAc.this, OthersHomepageAc.class)
                            .putExtra("uid", mList.get(position).getId()));
                }
            }
        });
    }

    /**
     * 附近的人
     */
    private void getNearData() {
        NearBody nearBody = new NearBody();
        nearBody.setP(String.valueOf(page));
        ApiUtil.getServiceApi().getNear(SPUtil.getToken(this),
                nearBody).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<NearBean>>() {
            @Override
            public void onSuccess(ApiResponse<NearBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            mList.addAll(data.getData().getInfo());
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        }));
    }


    @OnClick(R.id.iv_back)
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

}
